﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Modding;

partial class BlockBehaviour : BlockScript
{

//    //斜盘方块
//    protected Block SwashplateBlock = new Block()
//    #region 斜盘参数
//            ///ID of the Block
//            .ID(670)

//            ///Name of the Block
//            .BlockName("SwashplateBlock")

//            ///Load the 3d model information
//            .Obj(new List<Obj> { new Obj("SwashplateBlockMod/Axis.obj", "SwashplateBlockMod/Axis.png", new VisualOffset(Vector3.one, Offset, Vector3.zero)) })

//            ///For the button that we will create setup the visual offset needed
//            .IconOffset(new Icon(2f, Offset, new Vector3(45, 0, -30)))

//            ///Script, Components, etc. you want to be on your block.
//            .Components(new Type[] { typeof(SwashplateScript) })

//            ///Properties such as keywords for searching and setting up how how this block behaves to other elements.
//            .Properties(new BlockProperties().SearchKeywords(new string[] { "Swashplate" })/*.Burnable(3f).CanBeDamaged(3)*/)

//            ///Mass of the block 0.5 being equal to a double wooden block
//            .Mass(0.3f)

//            ///Display the collider while working on the block if you wish, then replace "true" with "false" when done looking at the colliders.
//            .ShowCollider(true)

//            ///Setup the collier of the block, which can consist of several different colliders.
//            ///Therefore we have this CompoundCollider,
//            .CompoundCollider(new List<ColliderComposite> { ColliderComposite.Capsule(0.25f, 1.25f, Direction.Z, Offset, Vector3.zero) })

//            ///Make sure a block being placed on another block can intersect with it.
//            .IgnoreIntersectionForBase()

//            ///Load resources that will be needed for the block.
//            .NeededResources(new List<NeededResource> { new NeededResource(ResourceType.Mesh, "SwashplateBlockMod/Hinge.obj") })

//            ///Setup where on your block you can add other blocks.
//            .AddingPoints(new List<AddingPoint> {
//                               (AddingPoint) new BasePoint(false, true)         //The base point is unique compared to the other adding points, the two booleans represent whether you can add to the base, and whether it sticks automatically.
//                                                .Motionable(true,false,false) //Set each of these booleans to "true" to Let the block rotate around X, Y, Z accordingly
//                                                .SetStickyRadius(0.5f),        //Set the radius of which the base point will connect to others
//                              new AddingPoint(new Vector3(0f, 0f, 0.75f), new Vector3(-90f, 0f, 0f),true).SetStickyRadius(0.3f)
//            });
//    #endregion



//    class SwashplateScript : BlockScript
//    {

//        bool NeedInstanceHinge = true;

//        //铰链数量菜单
//        MMenu HingesMenu;

//        /// <summary>
//        /// 变距铰数量
//        /// </summary>
//        enum HingeNumber
//        {
//#if DEBUG
//            //one = 1,
//#endif
//            two = 2,
//            three = 3,
//            four = 4,
//            five = 5,
//            six = 6,
//            seven = 7,
//            eight = 8
//        }

//        List<GameObject> VariablePitchHinges;

//        GameObject triggerForJointTemplate;

//        ConfigurableJoint CJ;

//        public override void SafeAwake()
//        {
//            GameObject wood = ModifiedAddBlock(BlockType.SingleWoodenBlock, new Vector3(0f, -1005f, 0f), Quaternion.identity);
//            triggerForJointTemplate = wood.transform.FindChild("TriggerForJoint2").gameObject;
//            Destroy(wood);

//            HingesMenu = AddMenu("number", 0, EnumToListstring<HingeNumber>());
//            HingesMenu.ValueChanged += (int value) => { ChangedHingeNumber(value + 2); };


//        }

//        List<GameObject> InstanceVariablePitchHinges(HingeNumber number)
//        {
//            List<GameObject> VariablePitchHinges = new List<GameObject>();

//            float instanceAngle;

//            for (int i = 0; i < (int)number; i++)
//            {
//                instanceAngle = 360 / (int)number * i;

//                GameObject VariablePitchHinge = new GameObject("VariablePitchHinge " + i);
//                VariablePitchHinge.transform.SetParent(transform);
//                VariablePitchHinge.transform.position = transform.position;
//                VariablePitchHinge.transform.localPosition += new Vector3(0, 0.5f, 1);
//                VariablePitchHinge.transform.rotation = transform.rotation;
//                VariablePitchHinge.transform.RotateAround(transform.position, transform.forward, instanceAngle);

//                GameObject hinge = new GameObject("Hinge");
//                hinge.transform.SetParent(VariablePitchHinge.transform);
//                hinge.transform.position = VariablePitchHinge.transform.position;
//                //hinge.transform.localPosition = VariablePitchHinge.transform.localPosition + new Vector3 (0,0.5f,1);
//                hinge.transform.rotation = VariablePitchHinge.transform.rotation;
//                //hinge.transform.RotateAround(transform.position, transform.forward, instanceAngle);

//                hinge.AddComponent<MeshFilter>().mesh = resources["SwashplateBlockMod/Hinge.obj"].mesh;
//                hinge.AddComponent<MeshRenderer>().material = new Material(Shader.Find("Transparent/Diffuse"));
//                hinge.GetComponent<MeshRenderer>().material.color = new Color(1, 1, 1, 0.5f);
//                hinge.AddComponent<Rigidbody>().isKinematic = true;

//                GameObject MC = new GameObject("HingeCollider ");
//                MC.AddComponent<MeshCollider>().sharedMesh = hinge.GetComponent<MeshFilter>().mesh;
//                MC.GetComponent<MeshCollider>().convex = true;
//                MC.transform.SetParent(VariablePitchHinge.transform);
//                MC.transform.position = hinge.transform.position;
//                MC.transform.rotation = hinge.transform.rotation;
//                //MC.transform.RotateAround(transform.position, transform.forward, instanceAngle);

//                AddPoint(VariablePitchHinge.transform, true, new AddingPoint(new Vector3(0, -0.25f, 0), new Vector3(0, 0, 180)).SetStickyRadius(0.2f));

//                //GameObject AD = AddPoint(VariablePitchHinge.transform, true, true, 0.2f);
//                //AD.transform.SetParent(VariablePitchHinge.transform);
//                //AD.transform.position = hinge.transform.position;
//                //AD.transform.rotation = hinge.transform.rotation;

//                //AD.transform.RotateAround(transform.position, transform.forward, instanceAngle);

//                //hinge.AddComponent<TriggerSetJoint2>();

//                VariablePitchHinges.Add(VariablePitchHinge);

//                Debug.Log("安装" + instanceAngle + "度变距铰");
//            }

//            return VariablePitchHinges;

//        }

//        void DestroyVariablePitchHinges()
//        {
//            if (VariablePitchHinges == null)
//            {
//                return;
//            }
//            foreach (var go in VariablePitchHinges)
//            {
//                DestroyImmediate(go);
//            }
//            VariablePitchHinges = null;
//            NeedInstanceHinge = true;
//        }

//        void ChangedHingeNumber(int value)
//        {
//            DestroyVariablePitchHinges();
//            VariablePitchHinges = InstanceVariablePitchHinges((HingeNumber)value);
//        }

//        protected override void BlockPlaced()
//        {
//            //AddPoint(new Vector3(1, 1, 1), Vector3.forward, true, 0.5f, true);

//            if (NeedInstanceHinge)
//            {
//                NeedInstanceHinge = false;
//                VariablePitchHinges = InstanceVariablePitchHinges((HingeNumber)HingesMenu.Value + 2);

//            }
//            Debug.Log(VariablePitchHinges);
//            Rigidbody.isKinematic = true;
//        }

//        protected virtual System.Collections.IEnumerator UpdateMapper()
//        {
//            if (BlockMapper.CurrentInstance == null)
//                yield break;
//            while (Input.GetMouseButton(0))
//                yield return null;
//            BlockMapper.CurrentInstance.Copy();
//            BlockMapper.CurrentInstance.Paste();
//            yield break;
//        }

//        public override void OnSave(XDataHolder stream)
//        {
//            base.OnSave(stream);
//            SaveMapperValues(stream);
//        }

//        public override void OnLoad(XDataHolder stream)
//        {
//            base.OnLoad(stream);
//            LoadMapperValues(stream);
//        }



//        void AddPoint(Vector3 Position, Vector3 Rotation, bool Sticky, float Radius, bool ShowCollider = false)
//        {
//            GameObject wood = ModifiedAddBlock(BlockType.SingleWoodenBlock, new Vector3(0f, -1005f, 0f), Quaternion.identity);
//            //wood.SetActive(false);
//            GameObject triggerForJointTemplate = wood.transform.FindChild("TriggerForJoint2").gameObject;

//            //GameObject triggerForJointTemplate = SwashplateMod.triggerForJointTemplate;
//            GameObject addingPoint = new GameObject("Adding Point");
//            addingPoint.transform.parent = gameObject.transform;
//            addingPoint.transform.localPosition = Position;
//            addingPoint.transform.localEulerAngles = Rotation;
//            BoxCollider addingPointCol = addingPoint.AddComponent<BoxCollider>();
//            addingPointCol.isTrigger = true;
//            addingPointCol.center = new Vector3(0f, -0.51f, 0f);
//            addingPointCol.size = new Vector3(0.6f, 0.0f, 0.6f);
//            addingPoint.layer = 12;

//            Material addingPointDisplayMaterial = new Material(Shader.Find("Transparent/Diffuse"));
//            addingPointDisplayMaterial.color = new Color(0.5f, 1f, 1f, 0.45f);

//            if (Sticky)
//            {
//                GameObject temp = new GameObject("Temp");
//                temp.transform.parent = addingPoint.transform;
//                temp.transform.localPosition = new Vector3(0f, -0.5f, 0f);
//                GameObject stickyJoint = (GameObject)Instantiate(triggerForJointTemplate, temp.transform.position, Quaternion.identity);
//                stickyJoint.name = "StickyJointTrigger";
//                stickyJoint.GetComponent<SphereCollider>().radius = Radius;
//                stickyJoint.transform.parent = gameObject.transform;
//                stickyJoint.GetComponent<TriggerSetJoint2>().myParent = gameObject.transform;
//                stickyJoint.GetComponent<TriggerSetJoint2>().myParentRigidbody = gameObject.GetComponent<Rigidbody>();

//                #region handle stickyPoint displaying
//                if (ShowCollider)
//                {
//                    GameObject stickyPointMesh = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//                    stickyPointMesh.name = "Sticky Joint Displayer";
//                    Destroy(stickyPointMesh.GetComponent<SphereCollider>());
//                    stickyPointMesh.transform.position = temp.transform.position;
//                    stickyPointMesh.transform.parent = addingPoint.transform;
//                    stickyPointMesh.transform.localScale = new Vector3(stickyJoint.GetComponent<SphereCollider>().radius, stickyJoint.GetComponent<SphereCollider>().radius, stickyJoint.GetComponent<SphereCollider>().radius);
//                    stickyPointMesh.GetComponent<MeshRenderer>().material = addingPointDisplayMaterial;
//                }
//                #endregion
//                Destroy(temp);
//            }

//            #region handle addingPoint displaying
//            if (ShowCollider)
//            {
//                GameObject addingPointMesh = GameObject.CreatePrimitive(PrimitiveType.Cube);
//                addingPointMesh.name = "Adding Point Displayer";
//                Destroy(addingPointMesh.GetComponent<BoxCollider>());
//                addingPointMesh.transform.parent = addingPoint.transform;
//                addingPointMesh.transform.localPosition = new Vector3(0f, -0.6f, 0f);
//                addingPointMesh.transform.localEulerAngles = Vector3.zero;
//                addingPointMesh.transform.localScale = new Vector3(0.6f, 0.0f, 0.6f);
//                addingPointMesh.GetComponent<MeshRenderer>().material = addingPointDisplayMaterial;
//            }
//            #endregion

//            Debug.Log("添加安装点");
//        }

//        void AddPoint(Transform block, bool ShowCollider, AddingPoint point)
//        {

//            GameObject addingPoint = new GameObject("Adding Point");
//            addingPoint.transform.parent = block.gameObject.transform;
//            addingPoint.transform.position = block.transform.position;
//            addingPoint.transform.rotation = block.transform.rotation;
//            addingPoint.transform.localPosition = point.position;
//            addingPoint.transform.localEulerAngles = point.rotation;
//            BoxCollider addingPointCol = addingPoint.AddComponent<BoxCollider>();
//            addingPointCol.isTrigger = true;
//            addingPointCol.center = new Vector3(0f, -0.51f, 0f);
//            addingPointCol.size = new Vector3(0.6f, 0.0f, 0.6f);
//            addingPoint.layer = 12;

//            Material addingPointDisplayMaterial = new Material(Shader.Find("Transparent/Diffuse"));
//            addingPointDisplayMaterial.color = new Color(0.5f, 1f, 1f, 0.45f);

//            if (point.sticky)
//            {
//                GameObject temp = new GameObject("Temp");
//                temp.transform.parent = addingPoint.transform;
//                temp.transform.localPosition = new Vector3(0f, -0.5f, 0f);
//                GameObject stickyJoint = (GameObject)Instantiate(triggerForJointTemplate, temp.transform.position, Quaternion.identity);
//                stickyJoint.name = "StickyJointTrigger";
//                stickyJoint.GetComponent<SphereCollider>().radius = point.radius;
//                stickyJoint.transform.parent = block.gameObject.transform;
//                stickyJoint.GetComponent<TriggerSetJoint2>().myParent = block.gameObject.transform;
//                stickyJoint.GetComponent<TriggerSetJoint2>().myParentRigidbody = block.gameObject.GetComponent<Rigidbody>();

//                #region handle stickyPoint displaying
//                if (ShowCollider)
//                {
//                    GameObject stickyPointMesh = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//                    stickyPointMesh.name = "Sticky Joint Displayer";
//                    Destroy(stickyPointMesh.GetComponent<SphereCollider>());
//                    stickyPointMesh.transform.position = temp.transform.position;
//                    stickyPointMesh.transform.parent = addingPoint.transform;
//                    stickyPointMesh.transform.localScale = new Vector3(stickyJoint.GetComponent<SphereCollider>().radius, stickyJoint.GetComponent<SphereCollider>().radius, stickyJoint.GetComponent<SphereCollider>().radius);
//                    stickyPointMesh.GetComponent<MeshRenderer>().material = addingPointDisplayMaterial;
//                }
//                #endregion
//                Destroy(temp);


//            }

//            #region handle addingPoint displaying
//            if (ShowCollider)
//            {
//                GameObject addingPointMesh = GameObject.CreatePrimitive(PrimitiveType.Cube);
//                addingPointMesh.name = "Adding Point Displayer";
//                Destroy(addingPointMesh.GetComponent<BoxCollider>());
//                addingPointMesh.transform.parent = addingPoint.transform;
//                addingPointMesh.transform.localPosition = new Vector3(0f, -0.6f, 0f);
//                addingPointMesh.transform.localEulerAngles = Vector3.zero;
//                addingPointMesh.transform.localScale = new Vector3(0.6f, 0.0f, 0.6f);
//                addingPointMesh.GetComponent<MeshRenderer>().material = addingPointDisplayMaterial;
//            }
//            #endregion
//            //return addingPoint;
//        }

//        GameObject AddPoint(Transform block, bool ShowCollider, bool Sticky, float Radius)
//        {

//            GameObject addingPoint = new GameObject("Adding Point");
//            addingPoint.transform.parent = block.gameObject.transform;
//            //addingPoint.transform.localPosition = point.position;
//            //addingPoint.transform.localEulerAngles = point.rotation;
//            BoxCollider addingPointCol = addingPoint.AddComponent<BoxCollider>();
//            addingPointCol.isTrigger = true;
//            addingPointCol.center = new Vector3(0f, -0.51f, 0f);
//            addingPointCol.size = new Vector3(0.6f, 0.0f, 0.6f);
//            addingPoint.layer = 12;

//            Material addingPointDisplayMaterial = new Material(Shader.Find("Transparent/Diffuse"));
//            addingPointDisplayMaterial.color = new Color(0.5f, 1f, 1f, 0.45f);

//            if (Sticky)
//            {
//                GameObject temp = new GameObject("Temp");
//                temp.transform.parent = addingPoint.transform;
//                temp.transform.localPosition = new Vector3(0f, -0.5f, 0f);
//                GameObject stickyJoint = (GameObject)Instantiate(triggerForJointTemplate, temp.transform.position, Quaternion.identity);
//                stickyJoint.name = "StickyJointTrigger";
//                stickyJoint.GetComponent<SphereCollider>().radius = Radius;
//                stickyJoint.transform.parent = block.gameObject.transform;
//                stickyJoint.GetComponent<TriggerSetJoint2>().myParent = block.gameObject.transform;
//                stickyJoint.GetComponent<TriggerSetJoint2>().myParentRigidbody = block.gameObject.GetComponent<Rigidbody>();

//                #region handle stickyPoint displaying
//                if (ShowCollider)
//                {
//                    GameObject stickyPointMesh = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//                    stickyPointMesh.name = "Sticky Joint Displayer";
//                    Destroy(stickyPointMesh.GetComponent<SphereCollider>());
//                    stickyPointMesh.transform.position = temp.transform.position;
//                    stickyPointMesh.transform.parent = addingPoint.transform;
//                    stickyPointMesh.transform.localScale = new Vector3(stickyJoint.GetComponent<SphereCollider>().radius, stickyJoint.GetComponent<SphereCollider>().radius, stickyJoint.GetComponent<SphereCollider>().radius);
//                    stickyPointMesh.GetComponent<MeshRenderer>().material = addingPointDisplayMaterial;
//                }
//                #endregion
//                Destroy(temp);


//            }

//            #region handle addingPoint displaying
//            if (ShowCollider)
//            {
//                GameObject addingPointMesh = GameObject.CreatePrimitive(PrimitiveType.Cube);
//                addingPointMesh.name = "Adding Point Displayer";
//                Destroy(addingPointMesh.GetComponent<BoxCollider>());
//                addingPointMesh.transform.parent = addingPoint.transform;
//                addingPointMesh.transform.localPosition = new Vector3(0f, -0.6f, 0f);
//                addingPointMesh.transform.localEulerAngles = Vector3.zero;
//                addingPointMesh.transform.localScale = new Vector3(0.6f, 0.0f, 0.6f);
//                addingPointMesh.GetComponent<MeshRenderer>().material = addingPointDisplayMaterial;
//            }
//            #endregion
//            return addingPoint;
//        }

//        private GameObject ModifiedAddBlock(BlockType block, Vector3 pos, Quaternion rot)
//        {
//            Transform transforms = ((GameObject)Instantiate(PrefabMaster.BlockPrefabs[(int)block].gameObject, pos, rot)).transform;
//            transforms.GetComponent<Rigidbody>().solverIterationCount = Game.AddPiece.woodBlockSolverIterationCount;

//            return transforms.gameObject;
//        }


//    }

//    public static List<string> EnumToListstring<T>()
//    {
//        List<string> list = new List<string>();

//        foreach (var v in Enum.GetValues(typeof(T)))
//        {
//            list.Add(v.ToString());
//        }
//        return list;

//    }

}
